package com.manlost.web.controller.baby;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.manlost.biz.ManlostServiceLocator;
import com.manlost.biz.baby.BabyService;
import com.manlost.biz.baby.model.dataobject.Baby;
import com.manlost.biz.baby.model.dataobject.BabyDraft;
import com.manlost.biz.baby.query.BabyQuery;
import com.manlost.biz.common.ibatis.PageQueryList;
import com.manlost.biz.tag.TagService;
import com.manlost.biz.tag.model.dataobject.Tag;
import com.manlost.biz.tag.model.dto.TagInfoDTO;
import com.manlost.web.common.FormateID;
import com.manlost.web.common.ManLostSession;
import com.manlost.web.common.Properties;
import com.manlost.web.common.WebUser;
import com.manlost.web.common.download.DownloadBabyTask;

@Controller
public class CollectionController {
	Log log=LogFactory.getLog(CollectionController.class);
	TagService tagService = ManlostServiceLocator.getTagService();
	BabyService babyService = ManlostServiceLocator.getBabyService();
	/*@RequestMapping(value="/collection",method = RequestMethod.GET)
	public String getRequest(Map<String,Object> model) {
		model.put("tags", tagService.listTagInfos());
		return "baby/collectionBabies";
    }*/
	
	@RequestMapping(value="/myCollections",method = RequestMethod.GET)
	public String myCollections(Map<String,Object> model) {
		WebUser webUser = ManLostSession.getCurrent().getWebUser();
		model.put("isSigned", webUser.isSignIn());
		BabyQuery query = new BabyQuery();
        query.setStartRow(0);
        query.setPageSize(30);
		PageQueryList<Baby> result = babyService.listBabys(query);
		model.put("babies", result);
		model.put("imageServer",Properties.PROTOL+Properties.IMAGE_SERVER+":"+Properties.IMAGE_SERVER_PORT);
		return "baby/myCollections";
	}
	
	@RequestMapping(value="/collectionTools",method = RequestMethod.GET)
	public String collectionTools(Map<String,Object> model) {
		WebUser webUser = ManLostSession.getCurrent().getWebUser();
		model.put("isSigned", webUser.isSignIn());
		return "baby/collectionTools";
	}
	
	@RequestMapping(value="/collection",method = RequestMethod.GET)
	public String getTags(Map<String,Object> model) {
		List<TagInfoDTO> tags=tagService.listTagInfos();
		StringBuffer sb=new StringBuffer("[");
		for(TagInfoDTO tagInfo:tags){
			sb.append("{\"id\":"+"\""+tagInfo.getTagGroup().getId()+"\",");
			sb.append("\"name\":"+"\""+tagInfo.getTagGroup().getName()+"\",");
			sb.append("\"tags\":[");
			for(Tag tag:tagInfo.getTags()){
				sb.append("{\"id\":"+"\""+tag.getId()+"\",");
				sb.append("\"name\":"+"\""+tag.getName()+"\"},");
			}
			sb=sb.deleteCharAt(sb.length()-1);
			sb.append("]},");
		}
		sb=sb.deleteCharAt(sb.length()-1);
		sb.append("]");
		model.put("tags",sb.toString()); 
		return "baby/collectionBabies";
	}
	
	@RequestMapping(value="/addCollection",method=RequestMethod.POST)
	ModelAndView add(Map<String,Object> model,@RequestParam("imageUrls")String imageUrls,@RequestParam("okTags")String okTags,HttpServletRequest request) {
		System.out.println(imageUrls+"--------"+okTags);
		List<BabyDraft> babyDrafts=null;
		if(StringUtils.isNotBlank(imageUrls)){
			String []urls=imageUrls.split(";");
			babyDrafts=new ArrayList<BabyDraft>(urls.length);
			for(int i=0;i<urls.length;i++){
				BabyDraft babyDraft=new BabyDraft();
				//ThumbnailUtil
				babyDraft.setUri(urls[i]);
				babyDraft.setBabyYear("2012");
				babyDraft.setBabyMonthDate("0401");
				//babyDraft.setImageSm("/2012/0401/"+id+".jpg");
				//babyDraft.setImageMi("/2012/0401/"+id+".jpg");
				babyDrafts.add(babyDraft);
			}
			babyService.inserBabyDrafts(babyDrafts);
			DownloadBabyTask task=new DownloadBabyTask();
			task.setDownloadTask(babyDrafts);
			task.doWork();
		}
		// 重定向需要重写 TODO
		String returnUrl = "http://www.manlost.com:9090/manlost/index.htm";
		return new ModelAndView("redirect:" + returnUrl);
		//return "baby/add";
    }
}
